using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using System.IO;
using Microsoft.Win32;

namespace CustomActions
{
	[RunInstaller(true)]
	public partial class VSAddInPath : Installer
	{
		public const string _regFolder = "Software\\Microsoft\\VisualStudio\\8.0\\AutomationOptions\\LookInFolders";

		public VSAddInPath()
		{
			InitializeComponent();
		}

		public override void Install(System.Collections.IDictionary stateSaver)
		{
			base.Install(stateSaver);

            Context.LogMessage("-------------------- VSAddInPath installer beginning --------------------");

			bool allUsersInstall = false;
			if (Context.Parameters.ContainsKey("allusers"))
			{
				try
				{
					allUsersInstall = int.Parse(Context.Parameters["allusers"]) > 0 ? true : false;
				}
				catch (Exception)
				{
					// Will happen if "allusers" property is invalid
				}
			}

			bool regInVS = false;
			if (Context.Parameters.ContainsKey("reginvs"))
			{
				try
				{
					regInVS = int.Parse(Context.Parameters["reginvs"]) > 0 ? true : false;
				}
				catch (Exception)
				{
					// Will happen if "reginvs" property is invalid
				}
			}

            if (regInVS)
            {
                try
                {
                    Context.LogMessage("Visual Studio add-in path installation:");
                    RegistryKey regRootKey = allUsersInstall ? Registry.LocalMachine : Registry.CurrentUser;
                    RegistryKey regKey = regRootKey.CreateSubKey(_regFolder);
                    Context.LogMessage("targetdir = " + Context.Parameters["targetdir"]);
                    Context.LogMessage("regRootKey = " + regKey.Name);
                    regKey.SetValue(Context.Parameters["targetdir"], "");
                    Context.LogMessage("Persisting install state");
                    stateSaver["registeredPath"] = Context.Parameters["targetdir"];
                    stateSaver["allUsersInstall"] = allUsersInstall.ToString();
                    Context.LogMessage("Successful");
                }
                catch (Exception excp)
                {
                    Context.LogMessage("Unsuccessful:");
                    Context.LogMessage(excp.Message);
                    Context.LogMessage(excp.StackTrace);
                }
            }
            else
            {
                Context.LogMessage("Skipping installation.");
            }

            Context.LogMessage("-------------------- VSAddInPath installer ending --------------------");
        }

		public override void Commit(System.Collections.IDictionary savedState)
		{
			base.Commit(savedState);
		}

		public override void Rollback(System.Collections.IDictionary savedState)
		{
			base.Rollback(savedState);
		}

		public override void Uninstall(System.Collections.IDictionary savedState)
		{
			base.Uninstall(savedState);

            Context.LogMessage("-------------------- VSAddInPath uninstaller beginning --------------------");

            if (savedState.Contains("registeredPath") &&
                savedState.Contains("allUsersInstall"))
            {
                try
                {
                    Context.LogMessage("Visual Studio add-in path removal:");
                    Context.LogMessage("registeredPath = " + savedState["registeredPath"].ToString());
                    bool allUsersInstall = bool.Parse(savedState["allUsersInstall"].ToString());
                    RegistryKey regRootKey = allUsersInstall ? Registry.LocalMachine : Registry.CurrentUser;
                    RegistryKey regKey = regRootKey.OpenSubKey(_regFolder, true);
                    Context.LogMessage("regRootKey = " + regKey.Name);
                    regKey.DeleteValue(savedState["registeredPath"].ToString());
                    Context.LogMessage("Successful");
                }
                catch (Exception excp)
                {
                    Context.LogMessage("Unsuccessful:");
                    Context.LogMessage(excp.Message);
                    Context.LogMessage(excp.StackTrace);
                }
            }
            else
            {
                Context.LogMessage("Nothing to uninstall.");
            }

            Context.LogMessage("-------------------- VSAddInPath uninstaller ending --------------------");
        }
	}
}